package com.hdax.dm.rest.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hdax.dm.entity.order.OrderCondition2Vo;
import com.hdax.dm.dao.order.DmOrderMapper;
import com.hdax.dm.entity.order.DmOrder;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(path = "/order")
public class DmOrderRestService extends ServiceImpl<DmOrderMapper,DmOrder> {

    //删除订单
    @PostMapping(path = "/deleteOrder")
    public int deleteOrder(@RequestParam("orderNo")String orderNo){
        QueryWrapper<DmOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("orderNo",orderNo);
        return baseMapper.delete(queryWrapper);
    }

    //添加订单
    @PostMapping(path = "/addOrder")
    public int addOrder(@RequestBody DmOrder dmOrder)throws Exception{
        return baseMapper.insert(dmOrder);
    }

    //查询订单
    @PostMapping(path = "/findOrder")
    public DmOrder findOrder(@RequestParam("orderNo")String orderNo){
        QueryWrapper<DmOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("orderNo",orderNo);
        return baseMapper.selectOne(queryWrapper);
    }

    //查询所有订单
    @PostMapping("findAllOrder")
    public List<DmOrder> findAllOrder(@RequestBody OrderCondition2Vo orderCondition2Vo){
        QueryWrapper<DmOrder> queryWrapper = new QueryWrapper<>();
        System.out.println(orderCondition2Vo);
        queryWrapper.eq("userId",orderCondition2Vo.getUserId());
        queryWrapper.eq(orderCondition2Vo.getOrderType()!=null,"orderType",orderCondition2Vo.getOrderType());
        queryWrapper.between(orderCondition2Vo.getEndTime()!=null&&orderCondition2Vo.getStartTime()!=null,"updatedTime",orderCondition2Vo.getStartTime(),orderCondition2Vo.getEndTime());
        queryWrapper.like(orderCondition2Vo.getKeyword()!=null&&!orderCondition2Vo.getKeyword().equals(""),"orderNo",orderCondition2Vo.getKeyword())
                .or().like("itemName",orderCondition2Vo.getKeyword());
        return baseMapper.selectList(queryWrapper);
    }

    //修改订单
    @PostMapping(path = "updateOrder")
    public int updateOrder(@RequestBody DmOrder dmOrder){
        return baseMapper.updateById(dmOrder);
    }

}
